<html>
<head><meta charset="utf-8"><title>zero registers · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html">zero registers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185416601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185416601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185416601">(Jan 12 2020 at 03:30)</a>:</h4>
<blockquote>
<p>Some registers are explicitly not supported for use with inline assembly:<br>
x86   k0  This is a constant zero register which can't be modified.</p>
</blockquote>
<p>Why can't the zero registers be <em>read</em> from?</p>



<a name="185425326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185425326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185425326">(Jan 12 2020 at 08:38)</a>:</h4>
<p>It doesn't make sense to use a zero register as an input or output.</p>



<a name="185425329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185425329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185425329">(Jan 12 2020 at 08:38)</a>:</h4>
<p>You are of course free to use the zero register inside your asm code.</p>



<a name="185445254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185445254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185445254">(Jan 12 2020 at 18:49)</a>:</h4>
<p>So should that RFC text be clarified to state something more precise than "Some registers are explicitly not supported <strong>for use with inline assembly</strong>"?</p>



<a name="185445266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185445266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185445266">(Jan 12 2020 at 18:49)</a>:</h4>
<p>As you say that we are "free to use the zero register".</p>



<a name="185446583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185446583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185446583">(Jan 12 2020 at 19:24)</a>:</h4>
<p>OK I will clarify the text.</p>



<a name="185447120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447120">(Jan 12 2020 at 19:39)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> they can be used in inline assembly</p>



<a name="185447121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447121">(Jan 12 2020 at 19:39)</a>:</h4>
<p>but the content of those registers is always zero</p>



<a name="185447164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447164">(Jan 12 2020 at 19:40)</a>:</h4>
<p>so setting such registers as inputs in an <code>asm!("...", in k0(foo))</code> expression is pointless, since <code>foo</code> won't be written to the <code>k0</code> register</p>



<a name="185447252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447252">(Jan 12 2020 at 19:41)</a>:</h4>
<p>the same applies to using them as outputs, such the content of the register after an inline assembly expression gets written to a Rust variable, since that just means that the variable will always be zeroed</p>



<a name="185447390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447390">(Jan 12 2020 at 19:45)</a>:</h4>
<p>Yes, that’s why I’m pointing this out. The text of the RFC says that those registers <strong>may not be used</strong> and now multiple people are trying to correct me and say they can be used. I think <span class="user-mention" data-user-id="143274">@Amanieu</span> understands the issue and is already going to clarify the mistake in the RFC.</p>



<a name="185447437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447437">(Jan 12 2020 at 19:46)</a>:</h4>
<p>The text does not say that they may not be used as input or output arguments, just that they may not be used at all.</p>



<a name="185447726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447726">(Jan 12 2020 at 19:55)</a>:</h4>
<p>Ah gotcha, yes, the text should be specific about using them in input and output arguments</p>



<a name="185447793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447793">(Jan 12 2020 at 19:57)</a>:</h4>
<p>This is why I started this topic with the direct quote.</p>



<a name="185447849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447849">(Jan 12 2020 at 19:58)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I was confused by your proposed wording:</p>
<blockquote>
<p>"Some registers are explicitly not supported for use with inline assembly"?</p>
</blockquote>



<a name="185447865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447865">(Jan 12 2020 at 19:58)</a>:</h4>
<p>But your last suggestion makes sense (e.g. "Some registers are explicitly not supported for use as input or output arguments" or something like that)</p>



<a name="185447965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185447965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185447965">(Jan 12 2020 at 20:00)</a>:</h4>
<p>Like, in RISC-V, if you want to implement a register to register copy (like an <code>x86</code> <code>mov</code>) the simplest way would be something like and <code>add</code> of the source register with <code>r0</code> (which is always zero) to the destination. So it is critically important that these "always zero" registers can be used within inline assembly.</p>



<a name="185448067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185448067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185448067">(Jan 12 2020 at 20:03)</a>:</h4>
<p>I have not proposed any alternate wording. The part you cite as confusing is <strong>from the RFC</strong></p>



<a name="185448115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185448115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185448115">(Jan 12 2020 at 20:04)</a>:</h4>
<p>Again, quoted at the beginning of the topic and then later with more bolding.</p>



<a name="185448131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185448131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185448131">(Jan 12 2020 at 20:05)</a>:</h4>
<p>I misread your second quote</p>



<a name="185448135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185448135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185448135">(Jan 12 2020 at 20:05)</a>:</h4>
<p>I thought the bolding was a proposed change</p>



<a name="185448315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185448315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185448315">(Jan 12 2020 at 20:11)</a>:</h4>
<p>The good thing here is that it seems we all agree the original phrasing was suboptimal.</p>



<a name="185451679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185451679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185451679">(Jan 12 2020 at 21:53)</a>:</h4>
<blockquote>
<p>so setting such registers as inputs in an <code>asm!("...", in k0(foo))</code> expression is pointless, since <code>foo</code> won't be written to the <code>k0</code> register</p>
</blockquote>
<p>I feel like I remember a few idioms in riscv that write to the 0 register and that make sense. Or maybe it was some other arch</p>



<a name="185451729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185451729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185451729">(Jan 12 2020 at 21:54)</a>:</h4>
<p>IIRC it was instructions that may write to multiple outputs and when you specify one of the outputs to be 0, you get some simplified instruction (same encoding)</p>



<a name="185524209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185524209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185524209">(Jan 13 2020 at 18:55)</a>:</h4>
<p>It seems like in that case, you still wouldn't use it as an <em>input</em> register. As best I can tell from your description, that would mean that you've already done the operation with multiple outputs outside the <code>asm!</code> block, then are trying to move one of the outputs into the zero register and call some more assembly.</p>
<p>As mentioned above, you should be able to use the zero registers <em>inside</em> the <code>asm!</code> block, so you could write <code>multioutput_op input1, input2, output1, $zero_reg;</code></p>



<a name="185604212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185604212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185604212">(Jan 14 2020 at 15:05)</a>:</h4>
<p>yeah, just using <code>x0</code> directly in assembly makes more sense.</p>



<a name="185604322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185604322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185604322">(Jan 14 2020 at 15:06)</a>:</h4>
<p>I remember also that <code>ld IMM</code> is really encoded as <code>add x0, IMM</code> IIRC. But again, just use <code>x0</code> directly</p>



<a name="185604385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185604385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185604385">(Jan 14 2020 at 15:07)</a>:</h4>
<p>LLVM could pass in <code>x0</code> regardless for your "reg" constraint for <code>in</code> variable if it knows variable is <code>0</code> too, but that’s transparent… or could it… nah, because you can modify the registers…</p>



<a name="185605762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185605762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185605762">(Jan 14 2020 at 15:22)</a>:</h4>
<p>GCC uses a separate constraint code for the zero register, which allows you to specify "zr" as the constraint code. This means: select the zero register if you can (the input value is 0) otherwise use a normal register.</p>



<a name="185605810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185605810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185605810">(Jan 14 2020 at 15:23)</a>:</h4>
<p>However LLVM doesn't support multiple constraints (it will always pick the most generic) so it will always select a normal register even if the input value is 0.</p>



<a name="185697264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185697264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185697264">(Jan 15 2020 at 13:33)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> I think you might be referring to RISC-V's <code>jal</code> instruction with <code>x0</code> as the output register to discard the return address ?<br>
cc <span class="user-mention" data-user-id="124289">@rkruppe</span> - they might now</p>



<a name="185697464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185697464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185697464">(Jan 15 2020 at 13:36)</a>:</h4>
<p>Disallowing zero registers in inputs/outputs seems to me to at least be the conservative choice. We can always allow that later if someone finds a good use for it, but  I can't think of any reason why using input/outputs with them would be better than just writing them in the text literal for the assembly</p>



<a name="185697566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/zero%20registers/near/185697566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/zero.20registers.html#185697566">(Jan 15 2020 at 13:37)</a>:</h4>
<p>For RISC-V it would be interesting to maybe specify whether the assembly literal supports pseudo-instructions like <code>j</code> or not - I don't see any reasons why not, all backends should support them anyways.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>